package com.mystnihon.tdroid.loader;

import java.io.IOException;

import android.content.Context;
import android.util.Log;
import ca.benow.transmission.TransmissionClient;

import com.mystnihon.tdroid.config.Config;

public class StartStopTorrentLoader extends AbstractAsyncLoader<Void> {
	private static final String TAG = StartStopTorrentLoader.class.getSimpleName();
	boolean idSetted = false;
	boolean mStart = false;
	Object[] mIds;
	TransmissionClient mclient;

	public StartStopTorrentLoader(TransmissionClient client, boolean startTorrent, Object... ids) {
		mStart = startTorrent;
		mIds = ids;
		mclient = client;
		idSetted = true;
	}

	@Override
	protected boolean doLoad(Context context) {
		if (!idSetted) {
			return false;
		}
		if (mStart) {
			try {
				mclient.startTorrents(mIds);
				return true;
			} catch (IOException e) {
				if (Config.WARNING_MODE_ENABLED) {
					Log.w(TAG, "Erreur while trying to start torrents : " + mIds, e);
				}
			}
		} else {
			try {
				mclient.stopTorrents(mIds);
				return true;
			} catch (IOException e) {
				if (Config.WARNING_MODE_ENABLED) {
					Log.w(TAG, "Erreur while trying to start torrents : " + mIds, e);
				}
			}
		}
		return false;
	}

}
